package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.provider.exceptions.DmcBizException;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.dto.oss.OptUploadFileReqDto;
import com.csun.cmny.provider.model.dto.oss.OptUploadFileRespDto;
import com.csun.cmny.provider.model.vo.OldmanRpcVo;
import com.csun.cmny.provider.service.OpcOssFeignApi;
import com.csun.cmny.provider.service.OpcRpcService;
import com.csun.cmny.util.wrapper.Wrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-11-06 20:43
 * @Description:
 */
@Slf4j
@Service
public class OpcRpcServiceImpl implements OpcRpcService {

    @Resource
    private OpcOssFeignApi opcOssFeignApi;

    /**
     * upload file
     *
     * @param optUploadFileReqDto the opt upload file req dto
     * @return the opt upload file resp dto
     */
    @Override
    public OptUploadFileRespDto uploadFile(OptUploadFileReqDto optUploadFileReqDto) {

        try {
            Wrapper<OptUploadFileRespDto> wrapper = opcOssFeignApi.uploadFile(optUploadFileReqDto);
            if (wrapper == null) {
                throw new DmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new DmcBizException(ErrorCodeEnum.DMC10068005,
                        optUploadFileReqDto.getUploadFileByteInfoReqDto().getFileName());
            }

            OptUploadFileRespDto result = wrapper.getResult();

            assert result != null;

            return result;
        } catch (Exception e) {
            log.error("uploadFile={}", e.getMessage(), e);
        }

        return null;
    }
}
